package lex;

import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import lex.title.mac.MacDecoration;

public class WindowDecoration extends LookAndFeel {
	public static String fontName = "Helvetica";
	private static Decoration DEFAULT_DECORATION = new MacDecoration();
	private static boolean DEFAULT_STICKY = true;
	// ==========================================
	private LookAndFeel inner = null;
	private Decoration decoration = null;
	private boolean sticky = DEFAULT_STICKY;

	public WindowDecoration() {
		super();
		inner = UIManager.getLookAndFeel();
		this.setDecoration(DEFAULT_DECORATION);
		UIManager.addPropertyChangeListener(new LookAndFeelChangeListener());
	}

	// =========================
	// Static Method
	// =========================
	public static void setDefaultDecoration(Decoration decoration) {
		DEFAULT_DECORATION = decoration;
	}

	public static void setSticky(boolean sticky) {
		DEFAULT_STICKY = sticky;
	}

	// =========================
	// Instance Method
	// =========================
	public void setDecoration(Decoration decoration) {
		this.decoration = decoration;
		LexRootPaneUI.setDecoration(decoration);
		this.getDefaults();
		JDialog.setDefaultLookAndFeelDecorated(true);
		JFrame.setDefaultLookAndFeelDecorated(true);
	}

	public void setDecoration(BuildinDecorations buildin) {
		this.setDecoration(buildin.getDecoration());
	}

	public String getID() {
		return "Lex";
	}

	public String getName() {
		return "Lex";
	}

	public String getDescription() {
		return "A Lightweight Java Look And Feel Provide the " + "Window Title Decoration.";
	}

	public boolean isNativeLookAndFeel() {
		return false;
	}

	public final boolean isSupportedLookAndFeel() {
		return true;
	}

	public boolean getSupportsWindowDecorations() {
		return true;
	}

	public UIDefaults getDefaults() {
		UIDefaults table = inner.getDefaults();
		this.initClassDefaults(table);
		this.initComponentDefaults(table);
		return table;
	}

	public Icon getDisabledIcon(JComponent component, Icon icon) {
		return this.inner.getDisabledIcon(component, icon);
	}

	public Icon getDisabledSelectedIcon(JComponent component, Icon icon) {
		return this.inner.getDisabledSelectedIcon(component, icon);
	}

	public LayoutStyle getLayoutStyle() {
		return this.inner.getLayoutStyle();
	}

	public void initialize() {
		this.inner.initialize();
	}

	public void provideErrorFeedback(Component component) {
		this.inner.provideErrorFeedback(component);
	}

	public void uninitialize() {
		this.inner.uninitialize();
	}

	protected void initClassDefaults(UIDefaults table) {
		table.put("RootPaneUI", LexRootPaneUI.class.getName());
	}

	protected void initComponentDefaults(UIDefaults table) {
		Decoration d = this.decoration;
		table.put("RootPane.colorChooserDialogBorder", d.getColorChooserDialogBorder());
		table.put("RootPane.errorDialogBorder", d.getErrorDialogBorder());
		table.put("RootPane.fileChooserDialogBorder", d.getFileChooserDialogBorder());
		table.put("RootPane.frameBorder", d.getFrameBorder());
		table.put("RootPane.informationDialogBorder", d.getInformationDialogBorder());
		table.put("RootPane.plainDialogBorder", d.getPlainDialogBorder());
		table.put("RootPane.questionDialogBorder", d.getQuestionDialogBorder());
		table.put("RootPane.warningDialogBorder", d.getWarningDialogBorder());
	}

	class LookAndFeelChangeListener implements PropertyChangeListener {
		public void propertyChange(PropertyChangeEvent e) {
			if (false == sticky)
				return;
			if (e.getPropertyName().equals("lookAndFeel")) {
				LookAndFeel current = UIManager.getLookAndFeel();
				if (current instanceof WindowDecoration)
					return;
				inner = current;
				// this will update with my defaults
				getDefaults();
				try {
					UIManager.setLookAndFeel(WindowDecoration.this);
				} catch (Exception ex) {
					// this should not happen
				}
			}
		}
	}
}
